<script src="/js/lib.js" type="text/javascript"></script>
<script src="/js/extjs-ux/app/ext-searchfield.js" type="text/javascript"></script>

<?php $translate=Zend_Registry::get('translate');?>

<script language="javascript">
Ext.onReady(function(){
	function doInsert(){
		insertWin.show();
	}

	function rowFormat(value, p, record) {
		var style='';
		return String.format(
        	'<div style='+style+'><b>{1}</b></div>',
        	value,record.data.name
        );
	}

	function doEdit() {		
		var m = myGrid.getSelectionModel().getSelections(); // get selected row of grid
		if(m.length > 0) {
			editWin.show();
			Ext.getCmp("editForm").getForm().load({ 
				url: '/role/primarykey/',
				params:{id: m[0].get('id')},
				waitMsg: 'Loading'
			});
		}
		else
			Ext.MessageBox.alert("<?php echo $translate->translate("Notice")?>", "<?php echo $translate->translate("Select data you want to edit")?>");
	}

	function doDelete() {		
		var m = myGrid.getSelectionModel().getSelections();
		if(m.length > 0) {
			Ext.MessageBox.confirm(
				'<?php echo $translate->translate("Notice")?>',
				'<?php echo $translate->translate("Are you sure you want to delete")?>',
				function(btn) {
					if (btn == 'yes') {
						Ext.Ajax.request({
							url: '/role/delete/', 
							params : {role_id: m[0].get('id')},
							method: 'POST',
							success: function (result, request){
								var res = new Object();
								res = Ext.util.JSON.decode(result.responseText);
								if (res.success == false)
									Ext.MessageBox.alert('<?php echo $translate->translate("Notice")?>', '<?php echo $translate->translate("Cannot delete members")?> '+ m[0].get("name")+'!');						
								else
								{
									Ext.Message.msg('<?php echo $translate->translate("Notice")?>', '<?php echo $translate->translate("Deleted data")?>');
									myGrid.getStore().reload();
								}
							}
						});	
					}
				}					
			);					
		}
		else
			Ext.MessageBox.alert('<?php echo $translate->translate("Notice")?>', '<?php echo $translate->translate("Select data you want to delete")?>');
	}	
	
	function doPermit() {		
		var m = myGrid.getSelectionModel().getSelections(); // get selected row of grid
		if(m.length > 0) {
			permitWin.show();			
			
			Ext.getCmp("permitForm").getForm().load({ 
				url: '/role/privilege/',
				params:{id: m[0].get('id')},
				waitMsg: 'Loading',
				success :function(form, action) {
					var res = new Object();
					res = Ext.util.JSON.decode(action.response.responseText);	
					var privilege = eval("["+res.data.privilege+"]");
					treePrivilegeRoot.expand(true, false, function() {
						treePrivilege.root.cascade(function(n) {
							treePrivilegeRoot.firstChild.expand(true);
							if(privilege.indexOf(parseInt(n.id))>=0)
								n.getUI().toggleCheck(true);
							else
							 	n.getUI().toggleCheck(false);
						})
					});					
				},
				failure:function(form, action) {
					var res = new Object();
					res = Ext.util.JSON.decode(action.response.responseText);
					Ext.Message.msg('<?php echo $translate->translate("Notice")?>', res.data.errors);
				}
			});	
		}
		else
			Ext.MessageBox.alert('<?php echo $translate->translate("Notice")?>', '<?php echo $translate->translate("Select data you want to grant")?>');
	}
	
	function btnEdit_Click(){
		editFrom.getForm().submit({
			waitMsg: 'Saving...',
			success: function (){
				editFrom.getForm().reset();
                editWin.hide();
                myGrid.getStore().reload();
                Ext.Message.msg('<?php echo $translate->translate("Notice")?>', '<?php echo $translate->translate("Successfully updated")?>');
            },
            failure: function () {
                 var res = new Object();
			     res = Ext.util.JSON.decode(action.response.responseText);
			   	 Ext.MessageBox.alert('<?php echo $translate->translate("Notice")?>', res.data.errors);
            }
        });
	}

	function btnInsert_Click(){
		insertFrom.getForm().submit({ 
	  		method:'POST',
	  		waitMsg: 'Saving...',
	  		success: function () {
	  			insertFrom.getForm().reset();
	        	insertWin.hide();
	        	myGrid.getStore().reload();
	        	Ext.Message.msg('<?php echo $translate->translate("Notice")?>', '<?php echo $translate->translate("Insert new success")?>');
	        },
	        failure:function(form, action){
	        	if(action.failureType == 'server'){
	            	obj = Ext.util.JSON.decode(action.response.responseText);
					Ext.Msg.alert('<?php $translate->translate("Notice")?>', obj.data.errors);
				}
	        }
        });
	}

	function btnEditPrivilege_Click(){
		var params = ''; 
		var selNodes = treePrivilege.getChecked();
		
        Ext.each(selNodes, function(node){
            if(params.length > 0){
                params += ',';
            }
            params += node.id;
        });

	 	permitForm.getForm().submit({
            waitMsg: 'Saving...',
            params: {privilegeId: params},
            success: function (){
                permitForm.getForm().reset();
                permitWin.hide();
                myGrid.getStore().reload();
                Ext.Message.msg('<?php echo $translate->translate("Notice")?>', '<?php echo $translate->translate("Successfully updated")?>');
            },
            failure: function () {
                Ext.MessageBox.alert ('<?php echo $translate->translate("Notice")?>', '<?php echo $translate->translate("Error")?>');
            }
        });
	}
	
	<?php echo $this->layout?>
});
</script>